package com.example.qing.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qing.config.AuthAccess;
import com.example.qing.config.LogType;
import com.example.qing.config.MyLogs;
import com.example.qing.pojo.*;
import com.example.qing.service.CommentService;
import com.example.qing.service.NovelService;
import com.example.qing.service.PhotoService;
import com.example.qing.utils.ProcessComments;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    NovelService novelService;
    @Resource
    CommentService commentService;
    @Resource
    ProcessComments processComments;

    @AuthAccess
    @GetMapping()
    public Result list(@RequestParam Integer novelId) {
        QueryWrapper<Comment>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("novel_Id",novelId);
        queryWrapper.orderByDesc("id");
        List<Comment>commentList = commentService.list(queryWrapper);
        List<Comment>result= processComments.processComments(commentList);
        return Result.success(result);
    }


    @MyLogs(operation = "评论",type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        commentService.removeById(id);
        return Result.success();
    }

    @AuthAccess
    @GetMapping("/total")
    public Result total(@RequestParam Integer novelId) {
        QueryWrapper<Comment>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("novel_id",novelId);
        long total=commentService.count(queryWrapper);
        return Result.success(total);
    }


    @MyLogs(operation = "评论",type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Integer> ids){
        log.info("批量删除");
        commentService.removeByIds(ids);
        return Result.success();
    }

    @AuthAccess
    @MyLogs(operation = "评论",type = LogType.ADD)
    @PostMapping("/add")
    public Result save(@RequestBody Comment comment){
        Novel novel= novelService.getById(comment.getNovelId());
        QueryWrapper queryWrapper=new QueryWrapper<Comment>();
        queryWrapper.eq("novel_id",comment.getNovelId());
        novel.setComments((int)commentService.count(queryWrapper)+1);
        novelService.updateById(novel);
        comment.setTime(DateUtil.now());
        commentService.save(comment);
        return Result.success();
    }

    @GetMapping("/selectByPage")
    public Result adminList(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        log.info("分页查询");
        Page<Comment> page=commentService.page(new Page<>(pageNum,pageSize));
        return Result.success(page);
    }







}
